﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;

namespace syc.Sale
{
    public partial class XiaoShou_FuKuan_In : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
             int BillID = 0;
             if (!this.IsPostBack)
             {
                 if (Request.QueryString["ID"] != null)
                 {
                     int.TryParse(Request.QueryString["ID"], out BillID);
                 }
                 if (BillID > 0)
                 {
                     SqlConnection Conn = new SqlConnection();
                     Conn.ConnectionString = ConfigurationManager.ConnectionStrings["SCMConnectionString1"].ConnectionString;
                     Conn.Open();
                     SqlCommand Cmd = new SqlCommand();
                     Cmd.Connection = Conn;
                     Cmd.CommandText = @"SELECT
                                                dbo.产品销售合同.ID,                                              
                                                dbo.销售到款记录.ID, 
                                                dbo.销售到款记录.收款单号, 
                                                dbo.销售到款记录.付款日期, 
                                                dbo.销售到款记录.收款, 
                                                dbo.销售到款记录.备注, 
                                                dbo.产品销售合同.收货单位ID, 
                                                dbo.产品订货单位.名称, 
                                                dbo.产品销售合同.金额,
                                                dbo.产品销售合同.合同编号
                                        FROM         
                                                dbo.产品销售合同 
                                        INNER JOIN
                                                dbo.销售到款记录 ON 
                                                dbo.产品销售合同.合同编号 = dbo.销售到款记录.合同编号
                                        INNER JOIN
                                                dbo.产品订货单位 ON 
                                                dbo.产品销售合同.收货单位ID = dbo.产品订货单位.ID
                                        WHERE dbo.销售到款记录.ID=" + BillID;

                     Cmd.CommandType = CommandType.Text;
                     SqlDataReader Reader = Cmd.ExecuteReader();
                     if (Reader.Read())
                     {
                         this.ViewState["BillID"] = BillID;
                         this.GongHuoDanWei.DataBind();

                         this.FaPiaoHao.Text = Reader.GetString(2).Trim();
                         this.FaPiaoHao.ReadOnly = false;
                         this.RiQi.Text = Reader.GetDateTime(3).ToString("yyyy年M月d日");
                         this.RiQi.ReadOnly = true;

                         //获取合同编号
                         string i = Reader.GetString(9);
                         this.HeTong.Text = Reader.GetString(9).ToString();
                         
                     

                         this.BenCiFuKuan.Text = Reader.GetDecimal(4).ToString();                      

                         this.BeiZhu.Text = Reader.GetString(5);
                         this.GongHuoDanWei.Text = Reader.GetString(7);
                         this.YingFuKuan.Text = Reader.GetDecimal(8).ToString();   
                         Reader.Close();
                         SqlCommand cmd1 = new SqlCommand("select SUM([收款])as 到款合计 from [销售到款记录]  where 合同编号='" + i + "'", Conn);
                         SqlDataReader sda1 = cmd1.ExecuteReader();
                         if (sda1.Read())
                         {
                             try
                             {

                                 Decimal 到款合计 = sda1.GetDecimal(0);
                                 this.YiFuJine.Text = 到款合计.ToString();//已付金额
                                 this.WeiFuJine.Text = (Decimal.Parse(this.YingFuKuan.Text) - 到款合计).ToString();//未付金额
                             }
                             catch (Exception ex)
                             {
                                 string t = ex.Message;
                                 this.YiFuJine.Text = "0.00";
                                 this.WeiFuJine.Text = this.YingFuKuan.Text;
                             }
                         }
                         sda1.Close();
                     }
                     else
                     {
                         Reader.Close();
                     }

                     Conn.Close();
                     Reader.Dispose();
                     Cmd.Dispose();
                     Conn.Dispose();
                 }
                 else
                 {
                     this.RiQi.Text = DateTime.Today.ToString("yyyy年M月d日");
                 }
             }
            
        }

        //保存按钮
        protected void Button_Finish_Click(object sender, EventArgs e)
        {
            SqlConnection Conn = new SqlConnection();
            Conn.ConnectionString = ConfigurationManager.ConnectionStrings["SCMConnectionString1"].ConnectionString;
            Conn.Open();
            SqlCommand Cmd = new SqlCommand();
            Cmd.Connection = Conn;
            SqlTransaction Tran = Conn.BeginTransaction();
            Cmd.Transaction = Tran;
            if (this.HeTong.Text != "" && this.GongHuoDanWei.Text != "")
            {
                try
                {
                    if (this.ViewState["BillID"] == null)
                    {
                        this.ViewState["BillID"] = -1;
                    }

                    Cmd.CommandText = "XiaoShouFuKuan_In";
                    Cmd.CommandType = CommandType.StoredProcedure;
                    Cmd.Parameters.Add(new SqlParameter("@收款单号", SqlDbType.NChar, 20, ParameterDirection.Input, true, 0, 0, "", DataRowVersion.Proposed, this.FaPiaoHao.Text));
                    Cmd.Parameters.Add(new SqlParameter("@付款日期", SqlDbType.Date, 10, ParameterDirection.Input, true, 0, 0, "", DataRowVersion.Proposed, DateTime.Parse(this.RiQi.Text)));
                    Cmd.Parameters.Add(new SqlParameter("@合同编号", SqlDbType.NVarChar, 20, ParameterDirection.Input, true, 0, 0, "", DataRowVersion.Proposed, this.HeTong.Text.Trim()));                   
                    Cmd.Parameters.Add(new SqlParameter("@付款方式", SqlDbType.NVarChar, 20, ParameterDirection.Input, true, 0, 0, "", DataRowVersion.Proposed, this.FuKuan.Text));

                    Cmd.Parameters.Add(new SqlParameter("@收款", SqlDbType.Money, 50, ParameterDirection.Input, true, 0, 0, "", DataRowVersion.Proposed, this.BenCiFuKuan.Text));                   
                    Cmd.Parameters.Add(new SqlParameter("@备注", SqlDbType.NVarChar, 20, ParameterDirection.Input, true, 0, 0, "", DataRowVersion.Proposed, this.BeiZhu.Text));
                    Cmd.Parameters.Add(new SqlParameter("@BillID", SqlDbType.Int, 4, ParameterDirection.Input, true, 0, 0, "", DataRowVersion.Proposed, this.ViewState["BillID"]));
                    Cmd.Parameters.Add(new SqlParameter("@ID", SqlDbType.Int, 4, ParameterDirection.Output, true, 0, 0, "", DataRowVersion.Proposed, null));
                    Cmd.ExecuteNonQuery();
                    this.Message.Text = "·保存成功";
                    Tran.Commit();                    
                 
                        
                    
                    
                }
                catch (Exception err)
                {
                    Tran.Rollback();
                    this.Message.Text = err.Message;
                    
                }               
            }
            
            Conn.Close();
            Tran.Dispose();
            Cmd.Dispose();
            Conn.Dispose();

            //如果保存成功则跳转到管理页面
            if (Message.Text == "·保存成功")
            {
                Response.Redirect("XiaoShou_FuKuan_In_Search.aspx");
            }
        }

        //返回按钮
        protected void Button_Modify_Click(object sender, EventArgs e)
        {
            Response.Redirect("XiaoShou_FuKuan_In_Search.aspx");
        }

        //合同号下拉别表变化后的处理逻辑代码
//        protected void HeTongHao_SelectedIndexChanged(object sender, EventArgs e)
//        {
//            this.RiQi.Text = "";
//            this.GongHuoDanWei.Text = "";
//            this.BeiZhu.Text = "";

//            SqlConnection Conn = new SqlConnection();
//            Conn.ConnectionString = ConfigurationManager.ConnectionStrings["SCMConnectionString1"].ConnectionString;
//            Conn.Open();
//            string cmd_tmp = @"SELECT                dbo.产品销售合同.ID, 
//                                                     dbo.产品销售合同.合同编号, 
//                                                     dbo.产品订货单位.名称,
//                                                     dbo.产品销售合同.金额
//                                            FROM     dbo.产品销售合同 INNER JOIN
//                                                     dbo.产品订货单位 ON 
//                                                     dbo.产品销售合同.收货单位ID = dbo.产品订货单位.ID
//                                            WHERE    dbo.产品销售合同.ID = ";
//            SqlCommand cmd = new SqlCommand(cmd_tmp + this.HeTongHao.SelectedValue, Conn);
//            SqlDataReader sda = cmd.ExecuteReader();
//            if (sda.Read())
//            {

//                String 收货单位 = sda.GetString(2);
//                this.GongHuoDanWei.Text = 收货单位;
//                Decimal yingfukuan = sda.GetDecimal(3);
//                this.YingFuKuan.Text = yingfukuan.ToString();
//            }
//            sda.Close();

//            SqlCommand cmd1 = new SqlCommand("select SUM([收款])as 到款合计 from [销售到款记录]  where [合同ID]=" + this.HeTongHao.SelectedValue, Conn);
//            SqlDataReader sda1 = cmd1.ExecuteReader();
//            if (sda1.Read())
//            {
//                try
//                {

//                    Decimal 到款合计 = sda1.GetDecimal(0);
//                    this.YiFuJine.Text = 到款合计.ToString();//已付金额
//                    this.WeiFuJine.Text = (Decimal.Parse(this.YingFuKuan.Text) - 到款合计).ToString();//未付金额
//                }
//                catch (Exception ex)
//                {
//                    string t = ex.Message;
//                    this.YiFuJine.Text = "0.00";
//                    this.WeiFuJine.Text = this.YingFuKuan.Text;
//                }
//            }


//            sda1.Close();
//            Conn.Close();
//        }
        protected void HeTong_TextChanged(object sender, EventArgs e)
        {
            SqlConnection Conn = new SqlConnection();
            Conn.ConnectionString = ConfigurationManager.ConnectionStrings["SCMConnectionString1"].ConnectionString;
            string sql = @"SELECT     dbo.产品销售合同.ID, dbo.产品销售合同.合同编号 as 编号, dbo.产品订货单位.名称, SUM(dbo.产品销售合同.金额) AS 应收金额, dbo.产品销售合同.金额, 
                      dbo.产品销售合同.日期
FROM         dbo.产品销售合同 INNER JOIN
                      dbo.产品订货单位 ON dbo.产品销售合同.订货单位ID = dbo.产品订货单位.ID
GROUP BY dbo.产品销售合同.ID, dbo.产品销售合同.合同编号, dbo.产品订货单位.名称, dbo.产品销售合同.金额, dbo.产品销售合同.日期";
            DataSet ds = new DataSet();
            SqlDataAdapter sda = new SqlDataAdapter(sql, Conn);
            sda.Fill(ds);
            DataView dv = ds.Tables[0].DefaultView;
            dv.RowFilter = "编号='" + this.HeTong.Text.Trim() + "'";
            int i = 0;
            string 合同ID = "";
            string 供货单位 = "";
            string 应收金额 = "0.00";
            foreach (DataRowView drv in dv)
            {
                i++;
                合同ID = drv["ID"].ToString();
                供货单位 = drv["名称"].ToString();
                应收金额 = drv["应收金额"].ToString();
                this.GongHuoDanWei.Text = 供货单位;
                this.YingFuKuan.Text = Decimal.Parse(应收金额).ToString("0.00");
            }
            if (i == 0)
            {
                this.Message.Text = "该合同不存在!";
                this.GongHuoDanWei.Text = "";
                this.YiFuJine.Text = "0.00";
                this.YingFuKuan.Text = "0.00";
                this.WeiFuJine.Text = "0.00";
            }
            Conn.Open();

            //SqlCommand cmd = new SqlCommand("select * from 应付金额 where 应付金额.合同编号='" + this.HeTongHao.SelectedItem.Text + "'", Conn);
            //SqlDataReader sda = cmd.ExecuteReader();
            //if (sda.Read())
            //{

            //    String 供货单位 = sda.GetString(2);
            //    this.GongHuoDanWei.Text = 供货单位.ToString();
            //    Decimal yingfukuan = sda.GetDecimal(3);
            //    this.YingFuKuan.Text = yingfukuan.ToString();
            //}
            //sda.Close();

            SqlCommand cmd1 = new SqlCommand("select SUM(本次付款)as 付款合计 from 采购付款登记  where 合同编号='" + this.HeTong.Text.Trim() + "'", Conn);
            SqlDataReader sda1 = cmd1.ExecuteReader();
            if (sda1.Read())
            {
                try
                {

                    Decimal 付款合计 = sda1.GetDecimal(0);
                    this.YiFuJine.Text = 付款合计.ToString("0.00");//已付金额
                    this.WeiFuJine.Text = (Decimal.Parse(this.YingFuKuan.Text) - 付款合计).ToString("0.00");//未付金额
                }
                catch (Exception ex)
                {
                    string t = ex.Message;
                    this.YiFuJine.Text = "0.00";
                    this.WeiFuJine.Text = this.YingFuKuan.Text;
                    this.Message.Text = "·准备就绪";
                }
            }


            sda1.Close();
            Conn.Close();
        }
    }

}
